package com.example.votemanage.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.votemanage.entity.VoteUser;
import com.example.votemanage.service.impl.VoteUserServiceImpl;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author xingcheng
 * @since 2024-06-06
 */
@Controller
public class VoteUserController {
    @Autowired
    VoteUserServiceImpl voteUserService;
    @RequestMapping("/login")
    public String login(String userid, String password, HttpSession session,HttpServletRequest request) {
        VoteUser voteUser=voteUserService.selectByNameAndPassword(userid,password);
        if(voteUser!=null){
            if(voteUser.getVuStatus()==1){
                request.setAttribute("msg","该用户已被禁用，请联系管理员");
                return "/login";
            }else {
                session.setAttribute("user", voteUser);
            }
        }else {
//            request.setAttribute("msg","用户名或密码错误");
            return "/login";
        }
        return "redirect:/index";
    }
    @PostMapping("/register")
    public String register(VoteUser voteUser){
            voteUser.setVuStatus(2);
            voteUser.setVuVersion(1);
            boolean saveResult = voteUserService.save(voteUser);
            if (saveResult) {
                return "redirect:login";
            } else {
                return "register";
            }
    }
    @RequestMapping("/register")
    public String register(){
        return "register";
    }
    @GetMapping("/logout")
    public String logout(HttpServletRequest request) {
        request.getSession().invalidate();
        return "redirect:/login";
    }
}
